﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using DVSProject.Lib;
using DVSProject.Models;

namespace DVSProject.Controllers
{
    public class MachineController : Controller
    {
        //
        // GET: /Machine/

        public ActionResult Index()
        {
            if (Session["User"] == null) { return RedirectToAction("LogOn", "Account"); }
            MachineRepo _MachineRepo = new MachineRepo();
            IEnumerable<MachineModels> _Machines = _MachineRepo.GetMachine();

            return View(_Machines);
        }

        //
        // GET: /Machine/Details/5

        public ActionResult Details(string id)
        {
            if (Session["User"] == null) { return RedirectToAction("LogOn", "Account"); }
            MachineRepo _MachineRepo = new MachineRepo();
            MachineModels _Machine = new MachineModels();
            _Machine = _MachineRepo.GetMachineById(id);
            return View(_Machine);
        }

        //
        // GET: /Machine/Create

        public ActionResult Create()
        {
            if (Session["User"] == null) { return RedirectToAction("LogOn", "Account"); }
            MachineModels _Machine = new MachineModels();
            ModelRepo _ModelRepo = new ModelRepo();
            IEnumerable<ModelModels> _Models = _ModelRepo.GetModel();

            int selectedModelId = 1;
            IEnumerable<SelectListItem> selectlist = from s in _Models
                                                     select new SelectListItem
                                                     {
                                                         Selected = (s.ModelId == selectedModelId),
                                                         Text = s.NameEng,
                                                         Value = s.ModelId.ToString()
                                                     };

            //_Machine.ModelMacs = _Models;
            _Machine.ModelMacSelect = selectlist;
            return View(_Machine);
        } 

        //
        // POST: /Machine/Create

        [HttpPost]
        public ActionResult Create(FormCollection collection)
        {
            if (Session["User"] == null) { return RedirectToAction("LogOn", "Account"); }
            try
            {
                // TODO: Add insert logic here
                MachineRepo _MachineRepo = new MachineRepo();
                MachineModels _Machine = new MachineModels();

                _Machine.SerialNo = collection["SerialNo"];
                _Machine.ModelId = SF.GetInt32(collection["ModelId"]);
                _Machine.Brand = collection["Brand"];
                _Machine.NameTh = collection["NameTh"];
                _Machine.NameEng = collection["NameEng"];
                _Machine.max_meter_no = SF.GetDouble(collection["max_meter_no"]);
                _Machine.start_meter_no = SF.GetDouble(collection["start_meter_no"]);
                _Machine.last_meter_no = SF.GetDouble(collection["last_meter_no"]);
                _Machine.CreateBy = (string)Session["User"];
                bool chekdup = _MachineRepo.CheckDub(_Machine.SerialNo);

                if (_Machine.SerialNo == "")
                {
                    TempData["errorname0"] = "ยังไม่ป้อนข้อมูล Serial No.";
                    return RedirectToAction("Create");
                }
                else if (chekdup)
                {
                    TempData["errorname0"] = "Serial No. มีการใช้งานแล้ว";
                    return RedirectToAction("Create");
                }
                else if (_Machine.NameTh == "")
                {
                    TempData["errorname1"] = "ยังไม่ป้อนข้อมูลชื่อเครื่องภาษาไทย";
                    return RedirectToAction("Create");
                }
                else if (_Machine.NameEng == "")
                {
                    TempData["errorname2"] = "ยังไม่ป้อนข้อมูลชื่อเครื่องภาษาอังกฤษ";
                    return RedirectToAction("Create");
                }
                else
                {
                    bool result = _MachineRepo.Create(_Machine);
                    return RedirectToAction("Index");
                }
            }
            catch
            {
                return View();
            }
        }
        
        //
        // GET: /Machine/Edit/5
 
        public ActionResult Edit(string id)
        {
            if (Session["User"] == null) { return RedirectToAction("LogOn", "Account"); }
            MachineRepo _MachineRepo = new MachineRepo();
            MachineModels _Machine = new MachineModels();
            _Machine = _MachineRepo.GetMachineById(id);

            ModelRepo _ModelRepo = new ModelRepo();
            IEnumerable<ModelModels> _Models = _ModelRepo.GetModel();

            int selectedModelId = 1;
            IEnumerable<SelectListItem> selectlist = from s in _Models
                                                     select new SelectListItem
                                                     {
                                                         Selected = (s.ModelId == selectedModelId),
                                                         Text = s.NameEng,
                                                         Value = s.ModelId.ToString()
                                                     };

            //_Machine.ModelMacs = _Models;
            _Machine.ModelMacSelect = selectlist;
            return View(_Machine);
        }

        //
        // POST: /Machine/Edit/5

        [HttpPost]
        public ActionResult Edit(string SerialNo, FormCollection collection)
        {
            if (Session["User"] == null) { return RedirectToAction("LogOn", "Account"); }
            try
            {
                // TODO: Add update logic here
                MachineRepo _MachineRepo = new MachineRepo();
                MachineModels _Machine = new MachineModels();
                _Machine.SerialNo = SerialNo;
                _Machine.ModelId = SF.GetInt32(collection["ModelId"]);
                _Machine.Brand = collection["Brand"];
                _Machine.NameTh = collection["NameTh"];
                _Machine.NameEng = collection["NameEng"];
                _Machine.max_meter_no = SF.GetDouble(collection["max_meter_no"]);
                _Machine.start_meter_no = SF.GetDouble(collection["start_meter_no"]);
                _Machine.last_meter_no = SF.GetDouble(collection["last_meter_no"]);
                _Machine.UpdateBy = (string)Session["User"];
                //  if (_Machine.SerialNo == "")
                //{
                //    TempData["errorname0"] = "ยังไม่ป้อนข้อมูล Serial No.";
                //    return RedirectToAction("Create");
                //}
                //else
                if (_Machine.NameTh == "")
                {
                    TempData["errorname1"] = "ยังไม่ป้อนข้อมูลชื่อเครื่องภาษาไทย";
                    return RedirectToAction("Create");
                }
                else  if (_Machine.NameEng == "")
                  {
                      TempData["errorname2"] = "ยังไม่ป้อนข้อมูลชื่อเครื่องภาษาอังกฤษ";
                      return RedirectToAction("Create");
                  }
                  else
                  {
                      bool result = _MachineRepo.Edit(_Machine);
                      return RedirectToAction("Index");
                  }
            }
            catch
            {
                return View();
            }
        }

        //
        // GET: /Machine/Delete/5
 
        public ActionResult Delete(string  id)
        {
            if (Session["User"] == null) { return RedirectToAction("LogOn", "Account"); }
            MachineRepo _MachineRepo = new MachineRepo();
            MachineModels _Machine = new MachineModels();
            _Machine = _MachineRepo.GetMachineById(id);
            return View(_Machine);
        }

        //
        // POST: /Machine/Delete/5

        [HttpPost]
        public ActionResult Delete(string SerialNo, FormCollection collection)
        {
            if (Session["User"] == null) { return RedirectToAction("LogOn", "Account"); }
            try
            {
                // TODO: Add delete logic here
                MachineRepo _MachineRepo = new MachineRepo();
                MachineModels _Machine = new MachineModels();
                _Machine.SerialNo = SerialNo;
                _Machine.UpdateBy = (string)Session["User"];
                bool result = _MachineRepo.Delete(_Machine);
                return RedirectToAction("Index");
            }
            catch
            {
                return View();
            }
        }
    }
}
